package net.lab1024.sa.admin.util.consumer;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.business.ws.MessageEndpoint;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 库存预警MQ消费者
 * @Author 1024创新实验室-主任:卓大
 * @Date 2023/7/28 19:39:21
 */
@Component
@Slf4j
public class StockWarnMQConsumer {
    // 注入WebSocket服务
    @Resource
    private MessageEndpoint messageEndpoint;

    private static final String STOCK_WARN_EXCHANGE = "stock_warn_exchange"; // 交换机名称
    private static final String STOCK_WARN_ROUTING_KEY = "stock.warn.key";    // 路由键
    private static final String STOCK_WARN_QUEUE = "stock_warn_queue";        // 队列名称

    // 监听指定队列
    @RabbitListener(queues = STOCK_WARN_QUEUE)
    public void handleStockWarnMessage(String warnMessage){
        log.info("收到库存预警消息,开始广播：{}", warnMessage);
        try {
            //执行webWebSocket广播
            messageEndpoint.broadcastAllUsers(warnMessage);
            log.info("库存预警消息广播成功");
        }catch (Exception e){
            log.error("库存预警消息处理失败：{}", e.getMessage());
        }
    }
}
